package com.fqcar.fqdf.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;

import com.fqdf.framework.android.util.AndroidUtils;
import com.fqdf.framework.android.util.CheckLogicUtil;


/**
 * @author 张枭
 * @version V1.0
 * @date 2017/6/19
 * @desc
 */
public class MyFlashUtils {

    private static Camera oldCamera;

    private static String cameraIDContainFlashUnit;

    private MyFlashUtils() {

    }


    public static void openFlash(Context context) {
        if (AndroidUtils.isSupportVersion(23)) {
            openFlashNewV(context);
        } else {
            openFlashOldV();
        }
    }

    public static void closeFlash(Context context) {
        if (AndroidUtils.isSupportVersion(23)) {
            closeFlashNewV(context);
        } else {
            closeFlashOldV();
        }
    }

    @TargetApi(23)
    private static void openFlashNewV(Context context) {
        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {

            String cameraID = getCameraIDContainFlashUnit(context);

            if (CheckLogicUtil.isEmpty(cameraID)) {
                DialogUtil.ToastMessage(context, "打开闪光灯失败");
            } else {
                manager.setTorchMode(cameraID, true);
            }

        } catch (CameraAccessException e) {
            e.printStackTrace();
            DialogUtil.ToastMessage(context, "打开闪光灯失败");
        }
    }

    @TargetApi(23)
    private static void closeFlashNewV(Context context) {
        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {

            String cameraID = getCameraIDContainFlashUnit(context);

            if (CheckLogicUtil.isEmpty(cameraID)) {
                DialogUtil.ToastMessage(context, "关闭闪光灯失败");
            } else {
                manager.setTorchMode(cameraID, false);
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
            DialogUtil.ToastMessage(context, "关闭闪光灯失败");
        }
    }

    /**
     * 获取包含闪光单元的camera
     *
     * @param context
     * @return
     * @throws CameraAccessException
     */
    @TargetApi(23)
    private static String getCameraIDContainFlashUnit(Context context) throws CameraAccessException {

        if (cameraIDContainFlashUnit == null) {
            CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
            if (manager == null || manager.getCameraIdList().length == 0) {

                return null;
            } else {
                for (String item : manager.getCameraIdList()) {
                    CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(item);
                    Boolean flashSupport = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                    if (flashSupport) {
                        cameraIDContainFlashUnit = item;
                        return cameraIDContainFlashUnit;
                    }
                }
            }
        }
        return cameraIDContainFlashUnit;
    }


    private static Camera openFlashOldV() {
        if (oldCamera == null) {
            oldCamera = Camera.open();
        }

        Camera.Parameters parameters = oldCamera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        oldCamera.setParameters(parameters);
        oldCamera.autoFocus(new Camera.AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) {
            }
        });
        oldCamera.startPreview();
        return oldCamera;
    }

    private static void closeFlashOldV() {
        if (oldCamera != null) {
            oldCamera.stopPreview();
            oldCamera.release();
            oldCamera = null;
        }
    }
}
